package cn.whdream.syn;

import java.util.Iterator;

public class SynDemo2 {
	
	public static void main(String[] args) throws InterruptedException {
		ThThread t = new ThThread(100);
		ThThread t1 = new ThThread(500);
		t.start();
		t1.start();
	}
	
	static class ThThread extends Thread {
		private int num;
		public ThThread(int num) {
			this.num = num;
		}
		@Override
		public void run() {
			try {
				System.out.println(Jvm.getInstance1(num));
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

}
class Jvm {
	private static Jvm jvm;
	
	private Jvm() {
	}
	public static synchronized Jvm getInstance(int num) throws InterruptedException { 
		synchronized(Jvm.class) {
			if (jvm == null) {
				Thread.sleep(num);
				jvm = new Jvm();
			}
			return jvm;
		}
	
	}
	public static Jvm getInstance1(int num) throws InterruptedException { 
		synchronized(Jvm.class) {
			if (jvm == null) {
				Thread.sleep(num);
				jvm = new Jvm();
			}
			return jvm;
		}
	
	}
}
